package nero.yncc.com.createart.activity;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.adapter.AdapterGoodsComment;
import nero.yncc.com.createart.base.BaseActivity;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanGoodsComment;
import nero.yncc.com.createart.utils.HttpUtils;

/**
 * Created by nero on 2017/7/12.
 */

@ContentView(R.layout.single_pulltorefresh)
public class ActivityComment extends BaseActivity {
    @ViewInject(R.id.pl_list)
    PullToRefreshListView pl_list;
    @ViewInject(R.id.ll_nodata)
    LinearLayout ll_nodata;

    AdapterGoodsComment adapterGoodsComment;
    ArrayList<BeanGoodsComment> arr_comment;

    String id;
    int page = 1, pageSize = 10;
    boolean canLoad = true;

    @Override
    protected void initData() {
        super.initData();
        setTitleBarTitle("评论");
        id = getIntent().getStringExtra("id");
        arr_comment = new ArrayList<>();
        adapterGoodsComment = new AdapterGoodsComment(mContext, arr_comment);
        pl_list.setMode(PullToRefreshBase.Mode.BOTH);
        pl_list.setAdapter(adapterGoodsComment);
    }

    @Override
    protected void initListener() {
        super.initListener();
        pl_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                arr_comment.clear();
                adapterGoodsComment.notifyDataSetChanged();
                canLoad = true;
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getData();
            }
        });
    }

    @Override
    protected void getData() {
        super.getData();
        //获取评论列表 取出3条就够咯
        HashMap<String, String> hashMap_comment = new HashMap<>();
        hashMap_comment.put("goods_id", id);
        hashMap_comment.put("p", page + "");
//        hashMap_comment.put("page_size", pageSize+"");

        new HttpUtils().post(this, Constants.API_GOODSCOMMENT, hashMap_comment, null, HttpUtils.TYPE_ARRAY, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                org.json.JSONArray jsonArray = (org.json.JSONArray) json;
                if (jsonArray == null || jsonArray.length() == 0) {
                    ll_nodata.setVisibility(View.VISIBLE);
                    pl_list.setVisibility(View.GONE);
                    pl_list.onRefreshComplete();
                    return;
                }

                if (!canLoad) {
                    showToast("没有更多内容啦");
                    pl_list.onRefreshComplete();
                    return;
                }

                if (jsonArray.length() < pageSize) {
                    canLoad = false;
                }

                for (int a = 0; a < jsonArray.length(); a++) {
                    BeanGoodsComment beanGoodsComment = null;
                    try {
                        beanGoodsComment = JSON.parseObject(jsonArray.getString(a), BeanGoodsComment.class);
                        if (arr_comment.size() < 3)
                            arr_comment.add(beanGoodsComment);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

                adapterGoodsComment.notifyDataSetChanged();
                pl_list.onRefreshComplete();
            }

            @Override
            public void fail(String failMsg) {
                pl_list.onRefreshComplete();
            }
        });
    }
}
